﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SiGeSIB.Data.Common;

namespace SiGeSIB.GUI.Common
{
	public partial class UDateTimeIntervalPicker : UserControl
	{
		#region Constructor
		public UDateTimeIntervalPicker()
		{
			InitializeComponent();

			dtpStart.Value = dtpEnd.Value = DateTime.Now;
		} 
		#endregion

		#region Properties
		[Browsable(false)]
		public DateTime? Start
		{
			get
			{
				return dtpStart.Checked ? (DateTime?)dtpStart.Value : null;
			}
			set
			{
				dtpStart.Checked = value.HasValue;
				if (dtpStart.Checked)
					dtpStart.Value = value.Value;
			}
		}
		[Browsable(false)]
		public DateTime? End
		{
			get
			{
				return dtpEnd.Checked ? (DateTime?)dtpEnd.Value : null;
			}
			set
			{
				dtpEnd.Checked = value.HasValue;
				if (dtpEnd.Checked)
					dtpEnd.Value = value.Value;
			}
		}

		[Browsable(false)]
		public DateTimeInterval Interval
		{
			get
			{
				DateTimeInterval interval = null;
				
				if (!Start.HasValue || !End.HasValue || Start.Value <= End.Value)
					interval = new DateTimeInterval(Start.Truncate(), End.Truncate());

				return interval;
			}
			set
			{
				if (value != null)
				{
					Start = value.Start;
					End = value.End;
				}
			}
		}

		public new bool Enabled
		{
			get
			{
				return dtpStart.Enabled || dtpEnd.Enabled;
			}
			set
			{
				dtpStart.Enabled = dtpEnd.Enabled = value;
			}
		}
		#endregion

		#region EventsHandlers
		public EventHandler IntervalChanged;
		private void dtpStart_ValueChanged(object sender, EventArgs e)
		{
			if (IntervalChanged != null)
				IntervalChanged(this, e);
		}

		private void dtpEnd_ValueChanged(object sender, EventArgs e)
		{
			if (IntervalChanged != null)
				IntervalChanged(this, e);
		} 
		#endregion
	}
}
